package com.zhou.core.logana.repository.redis;

import com.zhou.core.redis.RedisSession;
import com.zhou.core.redis.RedisSessionService;
import com.zhou.core.logana.LogAnaResult;
import com.zhou.core.logana.LogAnaService;
import com.zhou.core.logana.LogRecord;
import com.zhou.core.logana.repository.ILogAnaRepository;
import com.zhou.util.PropertyFileReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * redis�洢�ӿ�
 * Created by zhouyongjun on 2020/6/12
 */
public abstract class AbstractRedisLogAnaRepository implements ILogAnaRepository {
    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Override
    public LogAnaResult saveToList(LogRecord... records) {
        logger.info("save records size= "+ records.length);
        RedisSession redisSession = null;
        if (records == null || records.length == 0) return LogAnaResult.RESULT_FAIL;
        try {
            redisSession = redisSession(false);

            for (LogRecord record : records)
            {
                String assembleRepositoryKey = LogAnaService.getInstance().assembleWriteRepositoryKey(record.getRepositoryKey());
                redisSession.rPush(assembleRepositoryKey,record.getValue());
            }
            return LogAnaResult.RESULT_SUCC;
        } catch (Exception e) {
            logger.error("",e);
            return LogAnaResult.RESULT_FAIL;
        } finally {
            try {
                if (redisSession != null) redisSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public LogAnaResult readFromList(String repositoryPoolName, PropertyFileReader propertyFileReader,String repositoryKey, String extraKey, String formatDateNoSpr, int fromIndex, int toIndex) {
        String assembleRepositoryKey = LogAnaService.getInstance().assembleReadRepositoryKey(repositoryKey,extraKey,formatDateNoSpr);
        logger.info("readFromList assembleRepositoryKey= "+ assembleRepositoryKey+",fromIndex="+fromIndex+",toIndex="+toIndex);
        RedisSession redisSession = null;
        try {
            redisSession = redisSession(repositoryPoolName,propertyFileReader,true);
            List<String> list  = redisSession.lRange(assembleRepositoryKey,fromIndex,toIndex);
            return LogAnaResult.createSucLogAnaResult("",list);
        } catch (Exception e) {
            logger.error("",e);
            return LogAnaResult.RESULT_FAIL;
        }
    }

    @Override
    public LogAnaResult readFromList(String repositoryKey,String extraKey,String formatDateNoSpr, int fromIndex,int toIndex) {
        String assembleRepositoryKey = LogAnaService.getInstance().assembleReadRepositoryKey(repositoryKey,extraKey,formatDateNoSpr);
        logger.info("readFromList assembleRepositoryKey= "+ assembleRepositoryKey+",fromIndex="+fromIndex+",toIndex="+toIndex);
        RedisSession redisSession = null;
        try {
            redisSession = redisSession(true);
            List<String> list  = redisSession.lRange(assembleRepositoryKey,fromIndex,toIndex);
            return LogAnaResult.createSucLogAnaResult("",list);
        } catch (Exception e) {
            logger.error("",e);
            return LogAnaResult.RESULT_FAIL;
        }
    }
    public RedisSession redisSession(boolean isAutoClose)
    {
        return
                new RedisSession(RedisSessionService.getInstance().getResource("logana",
                        LogAnaService.getInstance().getConfig().getRespoitoryPropertyFileReader()
                ),isAutoClose, RedisSession.RedisConnectionMode.POOL);
    }

    public RedisSession redisSession(String repositoryPoolName,PropertyFileReader propertyFileReader,boolean isAutoClose)
    {
        return
                new RedisSession(RedisSessionService.getInstance().getResource(repositoryPoolName,
                        LogAnaService.getInstance().getConfig().getRespoitoryPropertyFileReader()
                ),isAutoClose, RedisSession.RedisConnectionMode.POOL);
    }
}


